const Router = require('koa-router')
const router = new Router({
    prefix: "/api/admin/activity"
})

const Activity = require('../../models/activity')
const {
    Success
} = require('../../utils/response')

router.get('/activity', async (ctx) => {
    let activityList = await Activity.findAll()
    ctx.body = Success(activityList)
})

router.post('/activity', async (ctx) => {

    let {
        name,
        img,
    } = ctx.request.body
    if (!name || !img) {
        ctx.body = Error('未携带指定参数')
    } else {
        let activity = await Activity.create({
            name,
            img,
            status: false
        })
        ctx.body = Success(activity, '添加成功');
    }


})

router.put('/activity', async (ctx) => {
    console.log(ctx.request.body);
    let {
        id,
        status
    } = ctx.request.body
    if (!id) {
        ctx.body = Error('未携带指定参数')
    } else {
        let user = await Activity.update({
            status,
        }, {
            where: {
                id
            }
        })
        if (user[0] == 1) {
            ctx.body = Success('', '修改成功');
        } else {
            ctx.body = Success('', '修改失败');
        }

    }


})

router.delete('/activity', async (ctx) => {

    let {
        id,
    } = ctx.request.body
    if (!id) {
        ctx.body = Error('未携带指定参数')
    } else {
        let user = await Activity.destroy({
            where: {
                id
            }
        })
        if (user == 1) {
            ctx.body = Success('', '删除成功');
        } else {
            ctx.body = Success('', '删除失败');
        }
    }

})

module.exports = router